package org.example.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.yc.damai.dto.Result;
import lombok.extern.slf4j.Slf4j;
import org.example.ZuulProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Component
@Slf4j
public class LoginInterceptesFilter extends ZuulFilter {

    @Autowired
    LoginFilter loginFilter;

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Autowired
    ZuulProperties zuulProperties;

    @Override
    public boolean shouldFilter() {
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        // /damai-user/login/login  /user/login/login
        String paths = "";
        for (String path : zuulProperties.getLoginInterceptes()) {
            if(paths.isBlank() == false){
                paths += "|";
            }
            paths += path;
        }
        String regex = "(" + paths + ")";
        String servletPath = request.getServletPath();
        log.debug("servletPath = " + servletPath);
        log.debug("regex = " + regex);
        log.debug("servletPath.matches(regex) = " + servletPath.matches(regex));
        return servletPath.matches(regex);
    }

    @Override
    public Object run() throws ZuulException {
        RequestContext context = RequestContext.getCurrentContext();
        HttpServletRequest request = context.getRequest();
        String loginToken = null;
        Result  result = new Result();
        for (Cookie cookie : request.getCookies()) {
            if(cookie.getName().equals("loginToken")){
                loginToken = cookie.getValue();
            }
        }
        if(loginToken == null){
            if(request.getHeader("Accept").startsWith("application/json")) {
                result.setCode(0);
                result.setMsg("请先登录系统");
                context.setSendZuulResponse(false);
                String json = null;
                try {
                    json = new ObjectMapper().writeValueAsString(result);
                } catch (JsonProcessingException e) {
                    throw new RuntimeException(e);
                }
                // 阻止请求到达目标资源
                context.setSendZuulResponse(false);
                context.getResponse().setContentType("application/json;charset=utf-8");
                context.setResponseBody(json);
            } else {
                try {
                    context.getResponse().sendRedirect("/login.html");
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        } else {
            // TODO 对于后续请求需要判断 loginToken
            // 正常访问目标资源
        }
        return null;
    }
}
